package cn.nawang.ebeim.client.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.nawang.ebeim.client.BackupThread;
import cn.nawang.ebeim.client.DataSourceThread;
import cn.nawang.ebeim.client.LocalBackupThread;
import cn.nawang.ebeim.client.RecoveryThread;
import cn.nawang.ebeim.client.TaskFetcherThread;
import cn.nawang.ebeim.client.UploadThread;

public class ThreadPoolUtil {

  private static final int DS_POOL_SIZE = 5;
  private static final int BACKUP_POOL_SIZE = 10;
  private static final int UPLOAD_POOL_SIZE = 5;
  private static final int RECOVERY_POOL_SIZE = 5;
  private static final int FETCHER_POOL_SIZE = 1;
  private static final int LOCAL_BACKUP_POOL_SIZE = 5;

  private static final int THREAD_POOL_SIZE =
      DS_POOL_SIZE + BACKUP_POOL_SIZE + UPLOAD_POOL_SIZE 
      + RECOVERY_POOL_SIZE + FETCHER_POOL_SIZE + LOCAL_BACKUP_POOL_SIZE;

  private static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);


  public static void startDataSourceThreads() {
    for (int i = 0; i < DS_POOL_SIZE; i++) {
      executorService.submit(new DataSourceThread());
    }
  }

  public static void startBackupThreads() {
    for (int i = 0; i < BACKUP_POOL_SIZE; i++) {
      executorService.submit(new BackupThread());
    }
  }

  public static void startUploadThreads() {
    for (int i = 0; i < UPLOAD_POOL_SIZE; i++) {
      executorService.submit(new UploadThread());
    }
  }

  public static void startRecoveryThreads() {
    for (int i = 0; i < RECOVERY_POOL_SIZE; i++) {
      executorService.submit(new RecoveryThread());
    }
  }
  
  public static void startTaskFetcherThreads() {
    executorService.submit(new TaskFetcherThread());
  }
  
  public static void startLocalBackupThreads() {
    for (int i = 0; i < LOCAL_BACKUP_POOL_SIZE; i++) {
      executorService.submit(new LocalBackupThread());
    }
  }
  
  


}
